const express = require('express');
const router = express.Router();
const Blog = require('../../mongoModuels/rear/blog');

router
.get('/message', (req, res) => {
    if(req.query.pageSize && req.query.pageNum) {
        let isType = {};
        if(req.query.keyWord) {
            let name = new RegExp(req.query.keyWord, 'i');
            isType = { '$or': [{ 'name': name }]};
        }
        Blog.blogMessage.estimatedDocumentCount({}, (err, count) => {
            if(err) {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            } else {
                let data = {
                    pageSize: parseInt(req.query.pageSize),
                    pageNum: parseInt(req.query.pageNum)
                }
                let query = Blog.blogMessage.find(isType);
                query.skip((data.pageNum - 1) * data.pageSize).limit(data.pageSize);
                query.exec({}, (err, rows) => {
                    if(err) {
                        res.json({
                            status: 0,
                            message: err
                        })
                    } else {
                        res.json({
                            status: 1,
                            message: '操作成功',
                            data: {
                                count,
                                rows
                            }
                        })
                    }
                })
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//查看

.post('/prohibit/message', (req, res) => {
    if(req.body.id) {
        if(req.body.curId) {
            Blog.blogMessage.findOne({'_id': req.body.id}).then(data => {
                let rows = [];
                data.children.forEach(item => {
                    if(item._id === req.body.curId) {
                        item.type = 2;
                    }
                    rows.push(item);
                });
                Blog.blogMessage.updateOne({'_id': req.body.id}, {'children': rows}).then(() => {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }).catch(() => {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        } else {
            Blog.blogMessage.updateOne({'_id': req.body.id}, {'type': 2}).then(() => {
                res.json({
                    status: 1,
                    message: '操作成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//禁用

.post('/enable/message', (req, res) => {
    if(req.body.id) {
        if(req.body.curId) {
            Blog.blogMessage.findOne({'_id': req.body.id}).then(data => {
                let rows = [];
                data.children.forEach(item => {
                    if(item._id === req.body.curId) {
                        item.type = 1;
                    }
                    rows.push(item);
                });
                Blog.blogMessage.updateOne({'_id': req.body.id}, {'children': rows}).then(() => {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }).catch(() => {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        } else {
            Blog.blogMessage.updateOne({'_id': req.body.id}, {'type': 1}).then(() => {
                res.json({
                    status: 1,
                    message: '操作成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//启用

.post('/delete/message', (req, res) => {
    if(req.body.id) {
        if(req.body.curId) {
            Blog.blogMessage.findOne({'_id': req.body.id}).then(data => {
                let rows = [];
                data.children.forEach((item, i, data) => {
                    if(item._id === req.body.curId) {
                        data.splice(i, 1);
                        rows = data;
                    }
                });
                Blog.blogMessage.updateOne({'_id': req.body.id}, {'children': rows}).then(() => {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }).catch(() => {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        } else {
            Blog.blogMessage.remove({'_id': req.body.id}).then(() => {
                res.json({
                    status: 1,
                    message: '操作成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//删除

module.exports = router;